
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="/css/public.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/js/common/jquery-3.4.1.js"></script>
<!--添加的form页面 start-->
<form class="layui-form" id="addForm" style="display: none;padding: 20px 20px 0 0 ">
    <div class="layui-form-item">
        <label class="layui-form-label">社团名称</label>
        <div class="layui-input-inline">
            <input type="text" name="a_name"  required  lay-verify="required" placeholder="请输入社团名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">社长学号</label>
        <div class="layui-input-inline">
            <input type="text" name="username"  required lay-verify="required" placeholder="请输入社长学号" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">12位</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-inline">
            <input type="text" name="creationtime" id="creationtime" readonly="readonly" required lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">社团人数</label>
        <div class="layui-input-inline">
            <input type="radio" name="number" value="1" title="创建时默认为1" checked />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">图片地址</label>
        <div class="layui-input-inline">
            <input type="text" name="a_url"  required lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">社团状态</label>
        <div class="layui-input-inline">
            <input type="radio" name="examine" value="1" title="等待审核" checked />
            <input type="radio" name="examine" value="0" title="审核通过"  />
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加的form页面 end-->


<button class="layui-btn" id="addCommunity">添加社团</button>
<table class="layui-hide" id="mycommunityList" lay-filter="test"></table>
<script>
    layui.use(['table','layer','form','laydate'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        //addForm表单的创建时间--start
        laydate.render({
            elem: '#creationtime',
            value: new Date() ,
            done: function(value, date){}
        });
        //addForm表单的创建时间--end

        //    --点击添加社团按钮 展示添加的弹出层 start--
        $("#addCommunity").click(function () {
            layer.open({
                type:1,     //弹出层类型
                title:'添加社团',
                area:'500px',   //宽度
                content:$("#addForm") //内容
        })
        })
        //    --点击添加社团按钮 展示添加的弹出层 end--

        //--添加功能--start
        //思路：1.给添加页面的添加按钮绑定表单提交事件  2.发送ajax请求，执行操作
        // 3.根据响应结果实现局部刷新（3.1隐藏弹出层  3.2 清空添加界面输入框数据 3.3 提示添加成功 3.4 表格重载）
        form.on('submit(add)',function (date) {

            $.ajax({
                url:'/u/ai/insert',
                data:$("#addForm").serialize(), //获取添加社团的请求参数
                dataType:'json',
                method:'post',
                success:function (result) {
                    if(result.flag){
                        layer.closeAll();       //关闭添加的界面弹出层
                        $("#addForm")[0].reset();   //使用js中的reset方法情况表单数据
                        layer.msg("添加成功");
                        table.reload('mycommunityList')
                    }
                    else {
                        alert(result.msg);
                    }},
                    error:function (data) {

                    }

                });

            return false;   // 页面不需要提交
        });
        //--添加功能--end





        //渲染（初始化）表格 start
        table.render({
            elem: '#mycommunityList'
            ,url:'/u/uai'
            // ,limits:8   //每页显示的记录数量
            // ,limits:[5,8,10,15,20]  //可选择每页显示的数量
            // ,page:true //开启分页
            // ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,method:'post'
            ,cols: [
                [
                    {field:'a_id', title: '编号', align:"center",width:100,sort:true}
                    ,{field: 'a_name', title: '社团名称', width:200}
                    ,{field: 'username', title: '社长学号', align:'center',width:150}
                    ,{field: 'creationtime', title: '创建时间',  align:'center',width:200,sort:true}
                    ,{field: 'number', title: '人数', align:'center',width:200,sort:true} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    ,{field: 'a_url', title: '社团图片', align:'center',width:200}
                    ,{field: 'examine', title: '状态', align:'center', minWidth:110, sort: true,templet: '#examine'}
                    ,{fixed:'right',title:'操作',width: '150',align:'center',templet:'#barDemo'}

                ]
            ]
        });
        //渲染（初始化）表格 end


    });

</script>
<!--自定义列模板 start-->
<script type="text/html" id="examine">
    {{#  if(d.examine == "1"){ }}
    <span style="color: #FF5722">等待审核</span>

    {{#  } else { }}
    审核通过
    {{#  }}}
</script>
<!--自定义列模板 end-->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

</script>



</body>
</html>